<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">

      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dataSearch.name" placeholder="请输入部门名称"></el-input>
      </el-form-item>

      <el-select
              v-model="dataSearch.school"
              placeholder="选择校区"
              prop="school"
      v-if='showSchool'>
        <el-option v-for="item in schoolList" :key="item._id" :label="item.name" :value="item._id"></el-option>
      </el-select>


      <el-form-item style="margin-left: 10px">
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
      <a href="#/department-new">
        <el-button type="primary" style="float:right" icon="el-icon-plus">新建部门</el-button>
      </a>
    </el-form>
    <el-table :data="dataList" border style="width: 100%">


      <el-table-column prop="name" label="部门名称"></el-table-column>
      <el-table-column prop="description" label="部门描述"></el-table-column>
      <el-table-column prop="school" label="校区"></el-table-column>
      <el-table-column prop="createdAt" label="创建时间"></el-table-column>



      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex";

export default {
  data() {
    return {

      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {
        name:undefined,
        school:undefined,


      },
      schoolList:undefined,
      limitSchool:undefined,
      showSchool :false
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info']),
  },
  methods: {


    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
      this.dataSearch.school = null
      this.getDataList(this.dataSearch)
    },
    handleClick(e) {
      this.$router.push({ path: "/department-detail?id=" + e._id });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    deleteClick(e) {
      this.$confirm("此操作将删除该部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
                request({
                  url: "/department/deleteDepartment",
                  method: "post",
                  data: {_id: e._id}
                }).then(res => {
                  if (res.code == 200) {
                    this.$message({
                      message: "删除成功"
                    });
                    this.getDataList(this.dataSearch);
                  }
                });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除"
                });
              });
    },
    getDataList(data) {
      this.limitSchool = this.info.school.map(value => value._id);
      data.limitSchool = this.limitSchool;
      request({
        url: "/department/getDepartmentList",
        method: "post",
        data: data
      }).then(res => {
        this.dataList = [];
        this.dataList = res.data.department;
        this.dataList.forEach((item, index) => {
          console.log(item)
          this.dataList[index].createdAt = this.formatDate(item.createdAt);
          this.dataList[index].school = item.school.name;

        });
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });
    },
    getSchoolList() {
      this.schoolList = this.info.school;

    },
    formatDate(date) {
      date = new Date(date);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    load(){
      this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.getSchoolList();
    this.getDataList(this.dataSearch);
      if ((this.info.school[0].headquarter== 1) || (this.info.school[0].headquarter== 2)){
        this.showSchool = true
      }
      this.rights = this.info.rights.map(function(e){return e.name})
      if (this.rights.indexOf('root') != -1){
        this.showSchool = true
      }
    }
  },

        beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
        beforeRouteLeave(to,from,next){
            next()
        },
  created() {
    this.load()
  }
};
</script>
